import streamlit as st

# set page width
st.set_page_config(layout="wide")

# st.title("Welcome to Xmake Analyzer")

import core.xmake as xmake

if 'xmanager' not in st.session_state:
    with st.sidebar.empty():
        with st.spinner('界面初始化中...'):
            st.session_state.xmanager = xmake.XmakeRepoAnalyzer()
            st.rerun()
xmanager = st.session_state.xmanager

Panel = [
    '本地安装的包',
    '官方仓库包(xrepo)',
    '制品信息',
]

p = st.sidebar.selectbox('选择类型', Panel)

if p == '本地安装的包':
    if name := st.sidebar.selectbox('Select Package', xmanager.pkgs()): 
        if ver := st.sidebar.selectbox('Select Version', xmanager.get_pkg_versions(name)):
            pkg = xmanager.get_pkg(name, ver)
            st.markdown(f'''
                ### {name}@{ver}
                    
                > 版本: {pkg['version']}  
                > 描述: {pkg['description']}  
                > 链接: {xmanager.help_get_pkg_homepage(name)}  
                ''')

            with st.expander('展开详情'):
                st.write(pkg)

elif p == '官方仓库包(xrepo)':
    if name := st.sidebar.selectbox('Select Package', xmanager.repo_pkgs()):
        pkg = xmanager.get_repo_pkg(name)
        st.subheader(p)

        st.markdown(f'''
                ### {name}
                    
                > 候选版本: {xmanager.help_get_pkg_versions(name)}  
                > 描述: {xmanager.help_get_pkg_description(name)}  
                > 链接: {xmanager.help_get_pkg_homepage(name)}  
                ''')

        with st.expander('展开包信息'):
            st.code(pkg, language='lua')

elif p == '制品信息':
    if name := st.sidebar.selectbox('Select Package', xmanager.repo_artifacts()):
        st.sidebar.info(f'制品 {name} 有 {len(xmanager.get_artifact_versions(name))} 个版本')
        if ver := st.sidebar.selectbox('Select Version', xmanager.get_artifact_versions(name)):
            artifact = xmanager.get_artifact(name, ver)
            st.subheader(p)

            with st.expander('展开包信息'):
                st.write(artifact)